const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 检查是否已存在日志管理菜单
    const [existingLogMenu] = await queryInterface.sequelize.query(
      `SELECT id FROM menus WHERE name = '日志管理' LIMIT 1`,
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    );

    if (existingLogMenu) {
      console.log('✅ 日志管理菜单已存在');
      return;
    }

    const logManagementId = uuidv4();
    const loginLogId = uuidv4();
    const auditLogId = uuidv4();

    // 直接创建顶级日志管理菜单及子菜单
    await queryInterface.bulkInsert('menus', [
      {
        id: logManagementId,
        parent_id: null,
        name: 'logs',
        title: '日志管理',
        path: '/logs',
        type: 'directory',
        icon: 'mdi:file-document-outline',
        order_num: 60,
        visible: true,
        status: true,
        cache: false,
        created_at: now,
        updated_at: now,
      },
      {
        id: loginLogId,
        parent_id: logManagementId,
        name: 'login-logs',
        title: '登录日志',
        path: '/logs/login',
        component: '/logs/login/index',
        type: 'menu',
        icon: 'mdi:login',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        created_at: now,
        updated_at: now,
      },
      {
        id: auditLogId,
        parent_id: logManagementId,
        name: 'audit-logs',
        title: '操作日志',
        path: '/logs/audit',
        component: '/logs/audit/index',
        type: 'menu',
        icon: 'mdi:text-box-outline',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        created_at: now,
        updated_at: now,
      },
    ]);

    console.log('✅ 日志管理菜单添加成功');
    console.log('   - 日志管理（父菜单）');
    console.log('   - 登录日志');
    console.log('   - 操作日志');
  },

  down: async (queryInterface) => {
    await queryInterface.sequelize.query(
      `DELETE FROM menus WHERE name IN ('日志管理', '登录日志', '操作日志')`
    );
  },
};

